package org.dromara.creditcard.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.core.exception.TransactionalException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MathUtil;
import org.dromara.common.core.utils.OrderUtil;
import org.dromara.creditcard.domain.*;
import org.dromara.creditcard.mapper.*;
import org.dromara.creditcard.service.ICreditOutRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 刷卡账单记录Service业务层处理
 *
 * @author Mr.Zhou
 * @date 2023-11-29
 */
@RequiredArgsConstructor
@Service
public class CreditOutRecordServiceImpl extends ServiceImpl<CreditOutRecordMapper, CreditOutRecord> implements ICreditOutRecordService {

    private final CreditOutRecordMapper outRecordMapper;

    private final CreditHolderMapper holderMapper;

    private final CreditMerMapper creditMerMapper;

    private final CreditCardMapper cardMapper;

    private final CreditInRecordMapper inRecordMapper;

    private final CreditBillMonthRecordMapper billMonthRecordMapper;

    @Override
    @Transactional
    public int addOutRecord(CreditOutRecord outRecord) {
        int ret = 0;
        try{
            CreditHolder holder = holderMapper.selectById(outRecord.getHolderId());
            CreditBillMonthRecord monthRecord = billMonthRecordMapper.selectById(outRecord.getMonthRecordId());
            CreditCard card = cardMapper.selectById(outRecord.getCardId());
            CreditMer posMer = creditMerMapper.selectById(card.getPosMerId());
            CreditMer qrMer = creditMerMapper.selectById(card.getQrMerId());
            outRecord.setAgentId(card.getAgentId());
            outRecord.setHolderName(holder.getRealName());

            String recordNo = OrderUtil.getOrderNoByTime();
            outRecord.setRecordNo(recordNo);

            Date repayDate = outRecord.getBillRepayDay();
            double money = outRecord.getPayMoney().doubleValue();
            double posRate = posMer.getPosRate().doubleValue();
            double posStandardRate = posMer.getPosStandardRate().doubleValue();
            double qrRate = qrMer.getQrRate().doubleValue();
            double qrStandardRate = qrMer.getQrStandardRate().doubleValue();
            double rate = 0;
            double standardRate = 0;
            if(outRecord.getIsQrPay() ==0){
                rate = posRate;
                standardRate = posStandardRate;
            }else{
                rate = qrRate;
                standardRate = qrStandardRate;
            }
//                outRecord.setPayMoney(outRecord.getPayMoney());//支付金额
//                outRecord.setBillMoney(new BigDecimal(money));//账单金额
//                outRecord.setPayRate(posMer.getPosRate());//还款费率
            //执行费率 手续费  当前还款金额 * 费率（实际扣除的）
            double fee = MathUtil.getDoubleUp(money * rate /100);
            outRecord.setPayFee(new BigDecimal(fee));
            //标准费率
            outRecord.setManageRate(new BigDecimal(standardRate));
            //标准费率的手续费（标准扣除的）
            double manageFee = MathUtil.getDoubleUp(money * standardRate /100);
            outRecord.setManageFee(new BigDecimal(manageFee));
            //标准 - 执行 = 剩余利润
            double profit = MathUtil.getDoubleUp(manageFee - fee);
            outRecord.setProfit(new BigDecimal(profit));
            outRecord.setIsDanger(0);
            outRecord.setIsDel(0);
            //还款日
            outRecord.setRepayDay(DateUtils.parseDate(monthRecord.getEndDate() + " 23:59:59"));

            outRecord.setFloatAmount(new BigDecimal(0));

            ret = outRecordMapper.insert(outRecord);
            if(ret <= 0){
                throw new TransactionalException("outRecordMapper insert error");
            }

            //手动添加刷卡记录  自动添加还款信息
            if(outRecord.getIsAddInRecord() == 1){

                CreditInRecord inRecord = new CreditInRecord();
                recordNo = OrderUtil.getOrderNoByTime();
                inRecord.setRecordNo(recordNo);
                inRecord.setMonthRecordId(outRecord.getMonthRecordId());
                inRecord.setCardId(card.getId());
                inRecord.setMonthDate(outRecord.getMonthDate());
                inRecord.setHolderName(holder.getRealName());
                inRecord.setPhoneNum(holder.getPhoneNum());
                inRecord.setCardNum(card.getCardNum());
                inRecord.setHolderId(card.getHolderId());
                inRecord.setRecordType(0);
                inRecord.setRepayMoney(new BigDecimal(money));//预计入账金额
                inRecord.setPayMoney(new BigDecimal(money));//实际支付金额
                //还款日
                inRecord.setRepayDay(DateUtils.parseDate(monthRecord.getEndDate() + " 23:59:59"));
//                //本账单还款日期
                inRecord.setBillRepayDay(repayDate);



                inRecord.setState(0);
                inRecord.setIsDanger(0);
                inRecord.setIsDel(0);
                inRecord.setCreateTime(new Date());
                ret = inRecordMapper.insert(inRecord);
                if(ret <= 0){
                    throw new TransactionalException("inRecordMapper insert error");
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("addOutRecord system error");
        }
        return ret;
    }
}
